export var treeData = []
var defaultChecked = []
import request from '@/utils/request.js'
import editorware from '../../utils/editorMiddleware'
import render from './renderEngine'
// import router from '@/router'
// export let editorSelectData = []
/**
 * 初始化树形控件数据
 */
export let coverageVm = ''
export function coverageVmFunc(vue) {
    coverageVm = vue
}
export function initTreeData(source) {
    // if (baseId) {
    //     sessionStorage.setItem('baseId', baseId)
    // }
    // coverageVm = vue
    return new Promise((resolve, reject) => {
        if (source == 'tree') {
            getTreeData(res => {
                if (res.length > 0) {
                    // console.log(editorSelectData, '00000000')
                    resolve({
                        data: treeData,
                        defaultChecked,
                        msg: '数据初始化成功'
                    })
                } else {
                    // reject('Data initialization failed. Tree menu is an empty array')
                    console.log('数据初始化失败')
                }
            })
        }
    })
}
/**
 * 处理树形控件数据
 * 拿到show为true的数据，默认选中
 */
function dispose() {
    if (treeData) {
        treeData.forEach(v => {
            if (v.children && v.children.length > 0) {
                recursion(v.children)
            }
        })
    }
}
/**
 * 采用递归函数处理子选项
 */
function recursion(data) {
    data.forEach(v => {
        if (v.show && !defaultChecked.includes(v.id)) {
            defaultChecked.push(v.id)
        }
        if (v.children && v.children.length > 0) {
            recursion(v.children)
        }
    })
}
/**
 * 获取树形控件数据
 */
function getTreeData(callBack) {
    if (sessionStorage.getItem('baseId')) {
        // 获取图层树结构
        request.get('api/map/baseFolder/getTreeFolderByBaseId', { baseId: sessionStorage.getItem('baseId') }).then(res => {
            if (res.code == 200) {
                treeData = []
                // editorSelectData = []
                res.data.map(item => {
                    treeData.push(item)
                    // editorSelectData.push(item)
                })
                // 处理图层菜单函数
                dispose()
                // 处理属性编辑框下拉菜单数据函数
                // editorSelectFunc()
                setTimeout(() => {
                    callBack(treeData)
                    // 获取地图图层数据
                    request.get('/api/map/draw/getDrawingByBaseId', { baseId: sessionStorage.getItem('baseId') }).then(res => {
                        if (res.code == 200 && res.data.length > 0) {
                            res.data.forEach(v => {
                                let addNode = {
                                    name: v.name,
                                    pid: v.pid,
                                    id: v.id,
                                    type: v.type,
                                    editor: true,
                                    show: true
                                }
                                let pNode = editorware.treeObj.getNode(v.pid)
                                if (pNode) {
                                    editorware.treeObj.append(addNode, pNode)
                                    // 设置新添加子菜单为选中状态
                                    if (v.isShow == '1') {
                                        editorware.treeObj.setChecked(addNode, true, false)
                                    }
                                    // 渲染数据
                                    render(v)
                                }
                            })
                        }
                    })
                }, 300)
            }
        })
    }
}
